#include "mainscene.h"
#include "config.h"
#include <QIcon>
#include <QPainter>
#include "map.h"
#include <QMouseEvent>
#include <ctime>
#include <QSound>
#include <QDebug>
#include <QMessageBox>
Mainscene::Mainscene(QWidget *parent)
    : QWidget(parent)
{
    //调用初始化场景
    initScene();
    //启动游戏
    playGame();

}

Mainscene::~Mainscene()
{
}

void Mainscene::initScene()
{
    //设置窗口固定尺寸

    setFixedSize(GAME_WIDTH,GAME_HEIGHT);

    //设置标题
    setWindowTitle(GAME_TITLE);
    //加载图标
    setWindowIcon(QIcon(GAME_ICON));

    //定时器设置
    m_Timer.setInterval(GAME_RATE);
    //敌机出场时间间隔初始化
    m_recorder = 0;
    //随机数种子
    srand((unsigned int)time(NULL));

}

void Mainscene::playGame()
{
    //启动背景音乐
    QSound::play(SOUND_BACKGROUND);
    //启动定时器
    m_Timer.start();
    //监听定时器发送的信号
    connect(&m_Timer , &QTimer::timeout,[=]()
    {
        //是否停止
               isStop();

               //更新元素
               updatePosition();

               //检测碰撞
               isHit();

               //更新绘制
               update();
    });
}

void Mainscene::updatePosition()
{
    //更新地图的坐标
    m_map.mapPosition();
    //飞机图片切换
       m_hero.picCycle();
    //发射子弹
    m_hero.shoot();
    //更新子弹坐标
       for(int i=0; i < BULLET_NUM; i++)
       {
           m_hero.m_Bullets[i].updatePosition();
       }
       //敌机放飞
           m_enemys.flyEnemys();
       //敌机更新
           m_enemys.updatePosition();

}

void Mainscene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    //绘制地图
    painter.drawPixmap(0,m_map.m_map1_posY , m_map.m_map1);
    painter.drawPixmap(0,m_map.m_map2_posY , m_map.m_map2);
    //绘制生命图标
        for(int i=0; i < m_hero.m_life; i++)
        {
            int temp_x = 0;
            int temp_y = GAME_HEIGHT - m_hero.m_Icon.height()*(i+1);
            painter.drawPixmap(temp_x, temp_y, m_hero.m_Icon);

        }

    //绘制敌机2
        for(int i=0; i < ENEMY2_NUM; i++)
        {
            if(!m_enemys.m_Enemy2[i].m_Free)
            {
                painter.drawPixmap(m_enemys.m_Enemy2[i].m_X, m_enemys.m_Enemy2[i].m_Y,
                                   *(m_enemys.m_Enemy2[i].p_Plane));
            }
            //绘制敌机2子弹
            for(int j=0; j < BULLET2_NUM; j++)
            {
                if(!m_enemys.m_Enemy2[i].m_Bullets[j].m_Free)
                {
                    painter.drawPixmap(m_enemys.m_Enemy2[i].m_Bullets[j].m_X,
                                       m_enemys.m_Enemy2[i].m_Bullets[j].m_Y,
                                       m_enemys.m_Enemy2[i].m_Bullets[j].m_Bullet);
                }
            }
        }
    //绘制敌机1
        for(int i=0; i < ENEMY1_NUM; i++)
        {
            if(!m_enemys.m_Enemy1[i].m_Free)
            {
                painter.drawPixmap(m_enemys.m_Enemy1[i].m_X, m_enemys.m_Enemy1[i].m_Y,
                                   *(m_enemys.m_Enemy1[i].p_Plane));
            }
        }
    //绘制英雄飞机
    painter.drawPixmap(m_hero.m_X,m_hero.m_Y,m_hero.m_Plane);

    //绘制子弹
        for(int i=0; i < BULLET_NUM; i++)
        {
            if(!m_hero.m_Bullets[i].m_Free)
            {
                painter.drawPixmap(m_hero.m_Bullets[i].m_X, m_hero.m_Bullets[i].m_Y,
                                   m_hero.m_Bullets[i].m_Bullet);
            }
        }


}

void Mainscene::mouseMoveEvent(QMouseEvent *event)
{
    int x = event->x() - m_hero.m_Rect.width()*0.5; //鼠标位置 - 飞机矩形的一半
    int y = event->y() - m_hero.m_Rect.height()*0.5;
    //边界检测
    if(x <= 0 )
    {
        x = 0;
    }
    if(x >= GAME_WIDTH - m_hero.m_Rect.width())
    {
        x = GAME_WIDTH - m_hero.m_Rect.width();
    }
    if(y <= 0)
    {
        y = 0;
    }
    if(y >= GAME_HEIGHT - m_hero.m_Rect.height())
    {
        y = GAME_HEIGHT - m_hero.m_Rect.height();
    }
    m_hero.setPosition(x,y);
}

/* 碰撞检测 */
void Mainscene::isHit()
{
    //子弹击中
    for(int i=0; i < BULLET_NUM; i++)
    {
        if(m_hero.m_Bullets[i].m_Free)
        {
           continue;
        }
        //击中敌机1
        for(int j=0; j < ENEMY1_NUM; j++)
        {
            if(m_enemys.m_Enemy1[j].m_Free || m_enemys.m_Enemy1[j].m_Down >= 0)
            {
                continue;
            }
            if(m_hero.m_Bullets[i].m_Rect.intersects(m_enemys.m_Enemy1[j].m_Rect))
            {
                //子弹设为闲置
                m_hero.m_Bullets[i].m_Free = true;
                //敌机爆炸
                m_enemys.m_Enemy1[j].m_Down = 0;
                //音效
                QSound::play(SOUND_BOMB);
            }
        }
        //击中敌机2
        for(int j=0; j < ENEMY2_NUM; j++)
        {
            if(m_enemys.m_Enemy2[j].m_Free || m_enemys.m_Enemy2[j].m_Down >= 0)
            {
                continue;
            }
            if(m_hero.m_Bullets[i].m_Rect.intersects(m_enemys.m_Enemy2[j].m_Rect))
            {
                //子弹设为闲置
                m_hero.m_Bullets[i].m_Free = true;
                //敌机生命减
                m_enemys.m_Enemy2[j].m_life--;

                if(m_enemys.m_Enemy2[j].m_life == 0)
                {
                    //爆炸
                    m_enemys.m_Enemy2[j].m_Down = 0;
                    //音效
                    QSound::play(SOUND_BOMB);
                    QSound::play(SOUND_BOMB);
                }
            }
        }

    }
    //飞机重生中直接返回
    if(m_hero.m_Down >= 0 && m_hero.m_Down <=HERO_TIME)
    {
        return;
    }
    //飞机碰撞敌机1
    for(int j=0; j < ENEMY1_NUM; j++)
    {
        if(m_enemys.m_Enemy1[j].m_Free || m_enemys.m_Enemy1[j].m_Down >= 0)
        {
            continue;
        }
        if(m_hero.m_Rect.intersects(m_enemys.m_Enemy1[j].m_Rect))
        {
            //爆炸
            m_enemys.m_Enemy1[j].m_Down = 0;
            m_hero.m_Down = 0;

            //音效
            QSound::play(SOUND_BOMB);
            QSound::play(SOUND_BOMB);
        }
    }
    //飞机碰撞敌机2
    for(int j=0; j < ENEMY2_NUM; j++)
    {
        //飞机碰撞子弹
        for(int k=0; k < BULLET2_NUM; k++)
        {
            if(m_enemys.m_Enemy2[j].m_Bullets[k].m_Free)
            {
                continue;
            }
            if(m_hero.m_Rect.intersects(m_enemys.m_Enemy2[j].m_Bullets[k].m_Rect))
            {
                //子弹设为闲置
                m_enemys.m_Enemy2[j].m_Bullets[k].m_Free = true;
                m_hero.m_Down = 0;
                QSound::play(SOUND_BOMB);
            }
        }
        if(m_enemys.m_Enemy2[j].m_Free || m_enemys.m_Enemy2[j].m_Down >= 0)
        {
            continue;
        }
        if(m_hero.m_Rect.intersects(m_enemys.m_Enemy2[j].m_Rect))
        {
            m_enemys.m_Enemy2[j].m_Down = 0;
            m_hero.m_Down = 0;

            //音效
            QSound::play(SOUND_BOMB);
            QSound::play(SOUND_BOMB);
        }
    }


}



/* 是否停止 */
void Mainscene::isStop()
{
    if(m_hero.m_life < 0)
    {
        m_Timer.stop();

        //对话框
        int isContinue = QMessageBox::question(this, "GAMEOVER", "是否继续游戏",
                              QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        if(isContinue == QMessageBox::Yes)
        {
            m_hero.m_life = HERO_LIFE;
            m_Timer.start();
        }
        else
        {
            exit(0);
        }
    }
}




